package com.greateamd.timetableasy.client.domain;

import com.greateamd.timetableasy.client.mvc.view.TimeTableasyPage;
import com.greateamd.timetableasy.domain.Auth;
import com.greateamd.timetableasy.domain.User;

public enum AuthType {
	
	ROLE_ADMIN(
			TimeTableasyPage.HOME,	
			TimeTableasyPage.ADD_USER,
			TimeTableasyPage.ADD_CAMPUS,
			TimeTableasyPage.ADD_COURSE,
			TimeTableasyPage.ADD_PROMOTION,
			TimeTableasyPage.ADD_CURSUS,
			TimeTableasyPage.ADD_UNIVERSITY,
			TimeTableasyPage.EDIT_USER,
			TimeTableasyPage.EDIT_UNIVERSITY,
			TimeTableasyPage.PLANNING_CAMPUS,
			TimeTableasyPage.PLANNING_PROMOTION,
			TimeTableasyPage.PLANNING_STUDYPERIOD,
			TimeTableasyPage.PLANNING_UNIVERSITY,
			TimeTableasyPage.PLANNING_USER,
			TimeTableasyPage.ADD_EVENT_USER,
			TimeTableasyPage.ADD_EVENT_SP,
			TimeTableasyPage.ADD_EVENT_UNIVERSITY),
	ROLE_USER(
			TimeTableasyPage.HOME,
			TimeTableasyPage.PLANNING_CAMPUS,
			TimeTableasyPage.PLANNING_PROMOTION,
			TimeTableasyPage.PLANNING_STUDYPERIOD,
			TimeTableasyPage.PLANNING_UNIVERSITY,
			TimeTableasyPage.PLANNING_USER,
			TimeTableasyPage.ADD_EVENT_USER),
	ROLE_CAMPUS_MANAGER(
			TimeTableasyPage.HOME,
			TimeTableasyPage.ADD_EVENT_CAMPUS,
			TimeTableasyPage.ADD_EVENT_PROMOTION,
			TimeTableasyPage.ADD_EVENT_SP,
			TimeTableasyPage.ADD_EVENT_USER,
			TimeTableasyPage.PLANNING_CAMPUS,
			TimeTableasyPage.PLANNING_PROMOTION,
			TimeTableasyPage.PLANNING_STUDYPERIOD,
			TimeTableasyPage.PLANNING_UNIVERSITY,
			TimeTableasyPage.PLANNING_USER),
	ROLE_TEACHER(
			TimeTableasyPage.HOME,
			TimeTableasyPage.ADD_EVENT_USER,
			TimeTableasyPage.PLANNING_CAMPUS,
			TimeTableasyPage.PLANNING_PROMOTION,
			TimeTableasyPage.PLANNING_STUDYPERIOD,
			TimeTableasyPage.PLANNING_UNIVERSITY,
			TimeTableasyPage.PLANNING_USER);

	final public TimeTableasyPage [] accessiblePages;
	
	private AuthType(TimeTableasyPage... accessiblePages) {
		this.accessiblePages = accessiblePages;
	}
	
	public static boolean authorizeUserPage(User user,TimeTableasyPage page){
		if(user== null || page == null)
			return false;
		for(Auth a :user.getAuths()){
			final AuthType auth = AuthType.valueOf(a.getAuthority());
			for(TimeTableasyPage p : auth.accessiblePages){
				if(p.equals(page))
					return true;
			}
		}
		return false;
	}
}
